<?php
session_start();  
include('../Model/db.php');
include('../Model/FacultyProject.php');

function getFacultyProjectById(){
	connectDB();
	$result = FacultyProject::getFacultyProjectByFaculty($_GET['idFaculty']);
	disconnectDB();
	echo json_encode($result);
}

function insertFacultyProject(){
	connectDB();
	autoCommitOff();
	FacultyProject::deleteFacultyProject($_POST['idFaculty']);
	$projectList = json_decode($_POST['projectList']);
	echo $projectList;
	foreach ($projectList as $idProject) {
		FacultyProject::insertFacultyProject($_POST['idFaculty'], $idProject);
	}
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

if($_POST){
	if(isset($_SESSION['wkadmin'])){
		if(isset($_POST['action'])){
			$action = $_POST['action'];
			switch($action){
				case 'insert' : insertFacultyProject(); break;
				default: echo json_encode(-1); break;
			}
		} else {
			echo json_encode(-1);
		}
	} else {
		echo json_encode(-1);
	}
}

if($_GET){
	if(isset($_GET['action'])){
		$action = $_GET['action'];
		switch($action){
			case 'getById' : getFacultyProjectById(); break;
			default: echo json_encode(-1); break;
		}
	} else {
		echo json_encode(-1);
	}
}
?>